﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MLRobotic.Tower.Detection;

namespace MLRobotic.Tower
{
    public partial class TargetColorControl : UserControl
    {
        private TargetColor _color;
        private bool _suspendUpdates = false;

        public TargetColorControl(TargetColor color)
        {
            _color = color;
            InitializeComponent();
            UpdateDisplay();
        }

        private void UpdateDisplay()
        {
            if (_color == null)
                return;

            try
            {
                _suspendUpdates = true;

                txtH.Text = _color.Hue.ToString();
                txtDH.Text = _color.DeltaHue.ToString();
                txtSmin.Text = _color.MinSaturation.ToString();
                txtSmax.Text = _color.MaxSaturation.ToString();
                txtLmin.Text = _color.MinBrightness.ToString();
                txtLmax.Text = _color.MaxBrightness.ToString();
            }
            finally
            {
                _suspendUpdates = false;
            }
        }

        private void txt_TextChanged(object sender, EventArgs e)
        {
            if (_color == null || _suspendUpdates)
                return;

            _color.Hue = double.Parse(txtH.Text);
            _color.DeltaHue = double.Parse(txtDH.Text);
            _color.MinSaturation = double.Parse(txtSmin.Text);
            _color.MaxSaturation = double.Parse(txtSmax.Text);
            _color.MinBrightness = double.Parse(txtLmin.Text);
            _color.MaxBrightness = double.Parse(txtLmax.Text);

            if (Parent != null && Parent.Parent != null && Parent.Parent.Parent != null)
            {
                var form = Parent.Parent.Parent as Form1;
                if (form != null)
                    form.Process();
            }
        }
    }
}
